---@class UIBattleRobListPanel : UIComponentBase
UIBattleRobListPanel = Class("",UIComponentBase)
local t = UIBattleRobListPanel

function t:OnCreate()
    self.Btn_Help = self:GetChild("Btn_Help")
    self.Btn_Refresh = self:GetChild("Btn_Refresh")
    self.Btn_BattleRecord = self:GetChild("Btn_BattleRecord")
    self.Btn_Def = self:GetChild("Btn_Def")
    self.robItemList = self:GetList("List_RobList")
    self.progressTip = self:GetChild("Text_RobList2")
    self.btn_onoff = self:GetChild("Btn_OnOff")
    self.btn_onoff_controller = self:GetController("OnOff",self.btn_onoff)
    self.txt_onoff_time = self:GetChild("Text_Time",self.btn_onoff)
    self.time_show_controller = self:GetController("Time_Show",self.btn_onoff)
    self.Text_FreeNum = self:GetChild("Text_FreeNum")
    self:SetDynamicList(self.robItemList,self.ItemRender)
    self:RegisterOnClick(self.Btn_Help,self.ClickHelp)
    self:RegisterOnClick(self.Btn_Refresh,self.ClickRefresh)
    self:RegisterOnClick(self.Btn_BattleRecord,self.ClickBattleRecord)
    self:RegisterOnClick(self.Btn_Def,self.ClickDef)
    self:RegisterOnClick(self.btn_onoff, self.onRobSwitch)
    self:RegisterEvent(Config.EventType.Refresh_PlunderInfo,self.RefreshPlunderInfoMsg)
    self:RegisterEvent(Config.EventType.Refresh_ChangePlunderFunction,self.ResUpdateOnOff)
    self:RegisterEvent(Config.EventType.BottomChangePage,self.BottomChangePage)
    UIRedDots.addViewListen(PanelNames.UIBattle, "Btn_BattleRecord", self.Btn_BattleRecord, Config.RedDotsType.WorldMapRevengeList) -- 世界地图复仇列表红点
    UIRedDots.addViewListen(PanelNames.UIBattle, "Btn_Def", self.Btn_Def, Config.RedDotsType.defFormation,EnumConst.FormationType.PlunderBattle) -- 世界地图防守阵容红点
end

function t:OnEnable()

end
function t:OnDisable()
end
function t:OnDestroy()
    self.dataList = nil
end
function t:Open()
    plunderHandler.sendReqPlunderInfo()
   -- plunderHandler.sendReqPlunderSearch(true)
    self:ResUpdateOnOff()
    SetPlayerPrefs("uibigmap_click_world_map", "1")
    FireEvent(Config.EventType.Fresh_WorldMap)
end
function t:Refresh()
    local cur_total_time = plunderHandler.rob_total_time or 0
    local up_time = dataTable.getGlobalInt(321)
    local cur_progress = cur_total_time/up_time
    if cur_progress > 1 then
        cur_progress = 1
    end
    cur_progress = math.ceil(cur_progress*100)
    -- this.bg_progress.value = cur_progress
    self.progressTip.text = string.format(getLanguage("BigMap_Rob_DailyLimit"),cur_progress)
    self:UpdateFreeRobNum()
end
---@param go GComponent
function t:ItemRender(index,go)
    local btn_head = go:GetChild("Btn_Head")
    local headIcon = btn_head:GetChild("head"):GetChild("HeadIcon")
    local headFrame = btn_head:GetChild("frame")
    local ctrlType = go:GetController("Type")   --收益大小
    local txt_power= go:GetChild("Text_Power")
    local txt_name = go:GetChild("Text_PlayerName")
    --local red_point = head:GetChild("red_point")--新
    local txt_time = go:GetChild("Text_Time")
    --local red_name_controller = head:GetController("RedName")
    local data = self.dataList[index]

    txt_name.text = data.name
    txt_time.text = getTimeStr(math.ceil(data.settle_time/1000))
    txt_power.text = HelperFunc.FormatResourceNum(data.total_power)
    self:SetPlayerIcon(headIcon,data.pic,headFrame,data.picFrame)
    local power_compare = dataTable.getGlobalString(337)
    local param_b = dataTable.getGlobalInt( 335)
    local rob_param1 = dataTable.getGlobalString( 361)
    local rob_param1_1 = rob_param1[1]/10000
    local rob_param1_2 = rob_param1[2]/10000
    param_b = param_b/10000
    local my_total_power = logicMgr.HeroManager.GetHerosSumPower()
    local rate = data.total_power/my_total_power
    local value2 = math.pow(rate, param_b)
    if value2 < rob_param1_1 then
        value2 = rob_param1_1
    elseif value2 > rob_param1_2 then
        value2 = rob_param1_2-0.0000001
    end
    for k, v in ipairs(power_compare) do
        if value2 >= v[1]/10000 and value2 < v[2]/10000 then
            ctrlType.selectedIndex = k-1
            break
        end
    end

    go.onClick:Clear()
    go.onClick:Add(function()
        plunderHandler.SetItemInfoFlagState( data.playerId, true )
        showUI(PanelNames.UIRob, data)
    end)
end
function t:ClickHelp()
    local txts = {};
    txts["title"] = "RogueHelpTitle01"
    txts["des"] = "RogueHelpDes01"
    showUI(PanelNames.CommonTips, txts);
end
function t:ClickRefresh()
    plunderHandler.sendReqPlunderInfo()
    --plunderHandler.sendReqPlunderSearch()
end
function t:ClickBattleRecord()
    showUI(PanelNames.UIRevengeRecord, 2)
end
function t:ClickDef()
    if not isOpenUI(PanelNames.UIBattleTeam) then
        local param = {}
        param.playerId = 0
        param.power = 0
        param.type = EnumConst.FormationType.PlunderBattle
        local def_formation = {}
        for i=1, 6 do
            def_formation[i] = plunderHandler.cur_def_formation[i]
        end
        param.formation = def_formation
        showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.Plunder, param, function(formations)
            local flag = true;
            if formations[1] ~= nil then
                for i = 1, #formations[1] do
                    if formations[1][i] ~= nil and tonumber(tostring(formations[1][i])) > 0 then
                        flag = false;
                        break ;
                    end
                end
            end
            if flag then
                UISysTips.AddMsg("需要上阵至少一名英雄");
            else
                for i = 1, 6 do
                    if formations[1][i] == nil then
                        formations[1][i] = 0
                    end
                end
                plunderHandler.sendReqChangePlunderFormation( formations[1] )
                closeUI(PanelNames.UIBattleTeam)
            end
        end)
    end
end
function t:RefreshPlunderInfoMsg()
    self.dataList = plunderHandler.GetCurPlunderSearchData()
    self.robItemList.numItems = #self.dataList
    self:Refresh()
    self:ResUpdateOnOff()
end
function t:UpdateFreeRobNum()
    local cur_times = plunderHandler.today_free_rob_times or 0
    local free_num = dataTable.getGlobalInt(339)
    local remain_times = free_num - cur_times
    if remain_times <= 0 then
        self.Text_FreeNum.text = ""
        return
    end
    self.Text_FreeNum.text = string.format(getLanguage("RogueTips08"), tostring(remain_times))
end

function t:ResUpdateOnOff()
    if self.time_id then
        utimer.remove(self.time_id)
        self.time_id = nil
    end
    self.txt_onoff_time.text = ""
    --print("ResUpdateOnOff------------------------>", plunderHandler.change_plunder_function_type, plunderHandler.last_change_func_time)
    if plunderHandler.change_plunder_function_type == 1 then

        self.btn_onoff_controller:SetSelectedIndex(0)
        if plunderHandler.last_change_func_time == 0 then
            self.time_show_controller:SetSelectedIndex(1)
        else
            self.time_show_controller:SetSelectedIndex(0)
            local remain_time = dataTable.getGlobalInt(363)
            local dis_time = plunderHandler.last_change_func_time + remain_time - Config.serverTime
            dis_time = math.ceil(dis_time/1000)
            if dis_time > 0 then
                self.time_show_controller:SetSelectedIndex(0)
                if self.txt_onoff_time then
                    local onoff_time_content
                    if dis_time > 60 then
                        local h, m, s = timeToTimeFormat2(dis_time)
                        onoff_time_content = string.format("%s:%s", h, m)
                    else
                        onoff_time_content = string.format("%s", dis_time)
                    end
                    self.txt_onoff_time.text = onoff_time_content
                    self.txt_onoff_time_content = onoff_time_content
                end

                self.time_id = utimer.timeCall(function()
                    dis_time = dis_time -1
                    if self.txt_onoff_time then
                        local onoff_time_content
                        if dis_time > 60 then
                            local h, m, s = timeToTimeFormat2(dis_time)
                            onoff_time_content = string.format("%s:%s", h, m)
                        else
                            onoff_time_content = string.format("%s", dis_time)
                        end
                        if self.txt_onoff_time_content == nil or self.txt_onoff_time_content ~= onoff_time_content then
                            self.txt_onoff_time.text = onoff_time_content
                            self.txt_onoff_time_content = onoff_time_content
                        end
                    end
                    if dis_time <= 0 then
                        if self.time_id then
                            utimer.remove(self.time_id)
                            self.time_id = nil
                        end
                        self.time_show_controller:SetSelectedIndex(1)
                        plunderHandler.change_plunder_function_type = 0
                        FireEvent(Config.EventType.Refresh_OpenPlunder,false)
                    end
                end, 1000, dis_time)
            else
                self.time_show_controller:SetSelectedIndex(1)
            end
        end
    else

        self.btn_onoff_controller:SetSelectedIndex(1)
        self.time_show_controller:SetSelectedIndex(1)
    end
end
function t:onRobSwitch()
    if plunderHandler.change_plunder_function_type == 1 then
        local remain_time = dataTable.getGlobalInt( 363)
        local dis_time = plunderHandler.last_change_func_time + remain_time  - Config.serverTime
        if dis_time > 0 then
            UISysTips.AddMsg(getLanguage("ErrorCode_172083"))
        else
            showUI(PanelNames.UIOpenPopup, 1)
        end
    else
        showUI(PanelNames.UIOpenPopup, 0)
    end
end
function t:BottomChangePage(index)
    if index ~= EnumConst.BottomPageType.battle then
        UIBattle.Refresh_OpenPlunder(false)
    end
end
return t